/*
 * @Author: lineCode 1145938682@qq.com
 * @Date: 2025-02-13 22:41:17
 * @LastEditors: lineCode 1145938682@qq.com
 * @LastEditTime: 2025-08-28 23:00:23
 * @FilePath: /backend/shorthand-modules/shorthand-modules-content/src/main/java/com/angus/modules/content/mapper/OrderCommissionMapper.java
 */
package com.angus.modules.content.mapper;

import com.angus.api.content.domain.OrderCommission;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;


/**
 * @author lineCode
 * @date 2025-08-17
 * @description 订单佣金映射器
 */
@Mapper
public interface OrderCommissionMapper extends BaseMapper<OrderCommission> {
    /**
     *
     * @param orderNo 订单号
     * @param courseCode 课程码
     * @return 金额
     */
    @Select("<script>" +
            "select " +
            "sum(commission_amount) as commissionAmount, " +
            "sum(course_amount) as courseAmount " +
            "from sys_order_commission " +
            "where 1=1 " +
            "<if test='orderNo != null and orderNo != &quot;&quot;'>and order_no = #{orderNo}</if> " +
            "<if test='courseCode != null and courseCode != &quot;&quot;'>and course_code = #{courseCode}</if> " +
            "</script>")
    Map<String, Object> sumCommission(
            @Param("orderNo") String orderNo,
            @Param("courseCode") String courseCode);

    /**
     * 订单佣金列表
     * @param orderNo 订单号（可选）
     * @param courseCode 课程码（可选）
     * @param offset 偏移量
     * @param limit 每页条数
     * @return 订单佣金列表
     */
    @Select("<script>" +
            "select " +
            "*, u.nick_name, c.course_name " +
            "from sys_order_commission " +
            "left join front_user u on sys_order_commission.user_id = u.user_id " +
            "left join sys_course c on sys_order_commission.course_code = c.course_code " +
            "where 1=1 " +
            "<if test='orderNo != null and orderNo != &quot;&quot;'>and order_no = #{orderNo}</if> " +
            "<if test='courseCode != null and courseCode != &quot;&quot;'>and course_code = #{courseCode}</if> " +
            "<if test='courseName != null and courseName != &quot;&quot;'>and c.course_name LIKE CONCAT('%', #{courseName}, '%')</if> " +
            "<if test='userId != null and userId != &quot;&quot;'>and sys_order_commission.user_id = #{userId}</if> " +
            "order by sys_order_commission.id desc " +
            "limit #{offset}, #{limit}" +
            "</script>")
    List<OrderCommission> getOrderCommissionList(
            @Param("orderNo") String orderNo,
            @Param("courseCode") String courseCode,
            @Param("courseName") String courseName,
            @Param("userId") Long userId,
            @Param("offset") int offset,
            @Param("limit") int limit);
}